package com.xiaomo.gitee.datamanager.service.tag.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiaomo.gitee.datamanager.aspect.log.anno.SysLog;
import com.xiaomo.gitee.datamanager.entity.*;
import com.xiaomo.gitee.datamanager.mapper.*;
import com.xiaomo.gitee.datamanager.service.tag.CategoryTagService;
import lombok.NoArgsConstructor;
import lombok.RequiredArgsConstructor;
import org.slf4j.MDC;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class CategoryTagServiceImpl implements CategoryTagService{

    private final CClassInfoMapper cClassInfoMapper;
    private final TagsMapper tagsMapper;
    private final ImageTagMapper imageTagMapper;
    private final ImagesMapper imagesMapper;
    private final CategoryTagMapper categoryTagMapper;
    private final Executor threadPool;


    @Override
    @SysLog("为数据集添加标签")
    public boolean addTagToSet(Long childClassId, List<Long> tagsId) {
        List<CategoryTag> list = categoryTagMapper.selectAllByChildClassId(childClassId);
        for (CategoryTag categoryTag : list) {
            if (tagsId.contains(categoryTag.getTagId())) {
                return false;
            }
        }

        List<CategoryTag> categoryTagList = tagsId.stream().map(tag -> new CategoryTag(childClassId,tag)).toList();
        categoryTagMapper.insertBatch(categoryTagList);
//        categoryTagMapper.insert(new CategoryTag(childClassId,tagsId));
        threadPool.execute(() -> {
            List<Images> imageList = imagesMapper.selectAllByClassification(childClassId);
            if (imageList.isEmpty()) {
                return;
            }

            List<ImageTag> imageTagList = new ArrayList<>();
            imageList.forEach(image -> {
                tagsId.forEach(tagId -> {
                    if (imageTagMapper.selectByImageIdAndTagId(image.getId(),tagId).isEmpty()) {
                        imageTagList.add(new ImageTag(image.getId(),tagId,true));
                    }
                });

            });
            imageTagMapper.insertBatch(imageTagList);
        });

        CClassInfo cClassInfo = cClassInfoMapper.selectOneById(childClassId);
        MDC.put("数据集",cClassInfo.getName());
        MDC.put("大类别id",cClassInfo.getP_class_id().toString());
        MDC.put("标签",tagsId.stream().map(tag -> tagsMapper.selectById(tag).getContent()).toList().toString());
        return true;
    }

    @Override
    @SysLog("删除数据集标签")
    public void deleteTagOfSet(Long childClassId, List<Long> tagsId) {

        threadPool.execute(() -> {
            List<Images> imageList = imagesMapper.selectAllByClassification(childClassId);
            imageList.forEach(image -> {
                QueryWrapper<ImageTag> queryWrapper = new QueryWrapper<>();
                queryWrapper.in("tag_id",tagsId);
                queryWrapper.eq("image_id",image.getId());
                queryWrapper.eq("is_belong_class",true);
                imageTagMapper.delete(queryWrapper);
//                imageTagMapper.deleteByImageIdAndIsBelongClassAndTagId(image.getId(),true,tagsId);
            });
        });

        QueryWrapper<CategoryTag> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("child_class_id",childClassId);
        queryWrapper.in("tag_id",tagsId);
        categoryTagMapper.delete(queryWrapper);
//        categoryTagMapper.deleteByTagIdAndChildClassId(tagsId,childClassId);

        CClassInfo cClassInfo = cClassInfoMapper.selectOneById(childClassId);
        MDC.put("数据集",cClassInfo.getName());
        MDC.put("大类别id",cClassInfo.getP_class_id().toString());
        MDC.put("标签",tagsId.stream().map((tag) -> tagsMapper.selectById(tag).getContent()).toList().toString());
    }

    @Override
    public List<Tags> getTagsOfSet(Long childClassId) {
        List<CategoryTag> list = categoryTagMapper.selectAllByChildClassId(childClassId);
        List<Tags> resultList;
        resultList = list.stream().map(CategoryTag::getTagId).toList().stream()
                .map(id -> tagsMapper.selectById(id)).collect(Collectors.toList());
        return resultList;
    }

    @Override
    public void deleteAllTagsOfSet(Long childClassId) {
        categoryTagMapper.deleteByChildClassId(childClassId);
    }
}




